package com.example.demo.util;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.CtrmLotDeliverRel;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.*;

public class TestSort {

    public static void main(String[] args) throws ParseException {
        List<CtrmLotDeliverRel> list = new ArrayList<>();

//        CtrmLotDeliverRel CtrmLotDeliverRel1 = new CtrmLotDeliverRel();
//        CtrmLotDeliverRel1.setContractLotId(1L);
//        CtrmLotDeliverRel1.setDeliverLotId(1L);
//        CtrmLotDeliverRel1.setType(2);
//        CtrmLotDeliverRel1.setContractLot("01");
//        CtrmLotDeliverRel1.setCreateTime(new Date());
//
//        CtrmLotDeliverRel CtrmLotDeliverRel2 = new CtrmLotDeliverRel();
//        CtrmLotDeliverRel2.setContractLotId(1L);
//        CtrmLotDeliverRel2.setDeliverLotId(1L);
//        CtrmLotDeliverRel2.setType(3);
//        CtrmLotDeliverRel2.setContractLot("02");

        CtrmLotDeliverRel CtrmLotDeliverRel3 = new CtrmLotDeliverRel();
        CtrmLotDeliverRel3.setContractLotId(1L);
        CtrmLotDeliverRel3.setDeliverLotId(1L);
        CtrmLotDeliverRel3.setCreateTime(new Date());
        CtrmLotDeliverRel3.setContractLot("0102");
        CtrmLotDeliverRel3.setLot(Long.valueOf("0102"));
        CtrmLotDeliverRel3.setType("0");

        CtrmLotDeliverRel CtrmLotDeliverRel4 = new CtrmLotDeliverRel();
        CtrmLotDeliverRel4.setContractLotId(1L);
        CtrmLotDeliverRel4.setDeliverLotId(1L);
        CtrmLotDeliverRel4.setCreateTime(new Date());
        CtrmLotDeliverRel4.setContractLot("0103");
        CtrmLotDeliverRel4.setLot(Long.valueOf("0103"));
        CtrmLotDeliverRel4.setType("2");

        CtrmLotDeliverRel CtrmLotDeliverRel5 = new CtrmLotDeliverRel();
        CtrmLotDeliverRel5.setContractLotId(1L);
        CtrmLotDeliverRel5.setDeliverLotId(1L);
        CtrmLotDeliverRel5.setCreateTime(new Date());
        CtrmLotDeliverRel5.setType("1");
        CtrmLotDeliverRel5.setContractLot("0101");
        CtrmLotDeliverRel5.setLot(Long.valueOf("0101"));

        //list.add(CtrmLotDeliverRel1);
        //list.add(CtrmLotDeliverRel2);
        list.add(CtrmLotDeliverRel3);
        list.add(CtrmLotDeliverRel4);
        list.add(CtrmLotDeliverRel5);
        System.out.println(list);

        List<CtrmLotDeliverRel> ctrmLotDeliverRels = sortList(list);
        String s = JSONObject.toJSONString(ctrmLotDeliverRels);
        //JSONObject result = JSONObject.parseObject(JSONObject.toJSONString(ctrmLotDeliverRels));


        System.out.println("result:"+s);

    }

    private static List<CtrmLotDeliverRel> sortList(List<CtrmLotDeliverRel> bookList) throws ParseException {
        String oriDate = "1900-01-01 00:00:00";
        Date oriDate1 = DateUtils.parseDate(oriDate, new String[]{"yyyy-MM-dd hh:mm:ss"});
        System.out.println("oriDate1:"+oriDate1);
        Collections.sort(bookList, new Comparator<CtrmLotDeliverRel>() {
            @Override
            public int compare(CtrmLotDeliverRel o1, CtrmLotDeliverRel o2) {
                // 进行判断
                Date createTime1 = o1.getCreateTime() ==null ? oriDate1 : o1.getCreateTime();
                Date createTime2 = o2.getCreateTime() ==null ? oriDate1 : o2.getCreateTime();
                int cp1 = (createTime1).compareTo(createTime2);
                String type1 = o1.getType();
                String type2 = o2.getType();
                if (type1.equals("2")) {
                    type1=("1");
                } else if (type1.equals("1")){
                    type1=("2");
                }
                if (type2.equals("1")) {
                    type2=("2");
                } else if (type2.equals("2")){
                    type2=("1");
                }

                int cp2 = (Integer.valueOf(type1)).compareTo(Integer.valueOf(type2));
                int cp3 = (o1.getContractLot()).compareTo(o2.getContractLot());
                //int cp3 = (o1.getLot()).compareTo(o2.getLot());
                if (cp1 == 0) {
                    if (cp2 == 0) {
                        if (cp3 == 0) {
                            return 0;
                        } else {
                            return cp3;
                        }
                    } else {
                        return cp2;
                    }
                } else {
                    return -cp1;
                }
            }
        });

        return bookList;
    }


}
